package com.s4j.struts.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.s4j.core.common.StringUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.hibernate.CommonDao;
import com.s4j.struts.biz.model.BizMgAppr;
import com.s4j.struts.biz.model.BizMgClinical;
import com.s4j.struts.biz.model.BizMgMark;
import com.s4j.struts.biz.model.BizMgSpecific;
import com.s4j.struts.biz.model.BizMgSupplement;
import com.s4j.struts.biz.model.BizMgSyndresult;
import com.s4j.struts.biz.model.BizMgTreathis;
import com.s4j.struts.biz.model.CombinedDisease;
import com.s4j.struts.biz.model.MgSuppImmun;
import com.s4j.struts.biz.model.MgSuppInspect;
import com.s4j.struts.biz.model.MgTreathisCm;
import com.s4j.struts.biz.model.PatientDoc;

@Service
public class MyastheniaDocService {
	
//	private static Logger log = Logger.getLogger(LiverDocService.class);
	
	@Resource
	private CommonDao dao;

	public DatagridPaging findUserByPaging(Integer page, Integer rows,
			PatientDoc patientDoc) {
		List param = new ArrayList();
		String hql = "from PatientDoc p where p.diseaseType = ? ";
		param.add(Constant.PATIENTDOC_DISEASETYPE_MG);
		if(patientDoc != null && !StringUtils.isNullOrBlank(patientDoc.getName())) {
			hql += " and p.name like ?";
			param.add('%' + patientDoc.getName() + '%');
		}
		param.add(page);
		param.add(rows);
		hql += " order by docId asc";
		DatagridPaging users = dao.findByHQLPaging(hql, param.toArray());
		return users;
	}

	public PatientDoc getPatientDoc(Long docId) {
		return dao.get(PatientDoc.class, docId);
	}

	public void saveCommon(PatientDoc patientDoc) {
		dao.save(patientDoc);
		patientDoc.setDocNo(String.valueOf(patientDoc.getDocId()));
		dao.update(patientDoc);
	}

	public void updateCommon(PatientDoc patientDoc) {
		dao.update(patientDoc);
	}

	public BizMgAppr getMgApprByPatientId(Long docId) {
		String hql = "from BizMgAppr t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgAppr.class, docId);
	}

	public BizMgSpecific getMgSpecificByPatientId(Long docId) {
		String hql = "from BizMgSpecific t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgSpecific.class, docId);
	}

	public BizMgTreathis getMgTreathisByPatientId(Long docId) {
		String hql = "from BizMgTreathis t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgTreathis.class, docId);
	}

	public BizMgMark getMgMarkByPatientId(Long docId) {
		String hql = "from BizMgMark t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgMark.class, docId);
	}

	public BizMgClinical getMgClinicalByPatientId(Long docId) {
		String hql = "from BizMgClinical t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgClinical.class, docId);
	}

	public BizMgSupplement getMgSupplementByPatientId(Long docId) {
		String hql = "from BizMgSupplement t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgSupplement.class, docId);
	}

	public BizMgSyndresult getMgSyndresultByPatientId(Long docId) {
		String hql = "from BizMgSyndresult t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizMgSyndresult.class, docId);
	}

	public void save(Object obj) {
		dao.save(obj);
	}
	
	public <T> T get(Class<T> clazz, Long id) {
		return dao.get(clazz, id);
	}

	public void update(Object obj) {
		dao.update(obj);
	}

	public void delete(Object obj) {
		dao.remove(obj);
	}

	public void deleteMgApprDiseases(BizMgAppr persis) {
		List<CombinedDisease> remove = persis.getDiseases();
		persis.setDiseases(null);
		
		for(int i=0; i<remove.size(); i++) {
			CombinedDisease disease = remove.get(i);
			disease.setMgAppr(null);
			dao.remove(disease);
		}
	}

	public void deleteMgTreaHistCm(BizMgTreathis persis) {
		List<MgTreathisCm> remove = persis.getCm();
		persis.setCm(null);
		
		for(int i=0; i<remove.size(); i++) {
			MgTreathisCm cm = remove.get(i);
			cm.setTreathis(null);
			dao.remove(cm);
		}
	}

	public MgSuppImmun findImmuBySuppWeek(Long suppId, int week) {
		String hql = "from MgSuppImmun m where m.supp.suppId = ? and m.week = ?";
		return dao.findByHQLUnique(hql, MgSuppImmun.class, suppId, week);
	}

	public MgSuppInspect findInspectBySuppWeek(Long suppId, int week) {
		String hql = "from MgSuppInspect m where m.supp.suppId = ? and m.week = ?";
		return dao.findByHQLUnique(hql, MgSuppInspect.class, suppId, week);
	}

}
